<?php

namespace app\api\controller\login;

use app\api\controller\Member;
use app\common\model\CourseCateModel;
use app\common\model\CourseCommentModel;
use app\common\model\CourseModel;
use app\common\model\CourseStateModel;
use app\common\model\UserModel;
use app\common\model\UserTimesModel;
use app\common\model\CourseAttendanceModel;

class Course extends Member
{
    /**
     * 获取教学风采列表
     */
    public function getOldCourse(){
        $user = $this->getMember();
        $course_state = CourseStateModel::where('school_id',$user['school_id'])
            ->where('process',2)
            ->order('sort asc,id desc')
            ->limit(4)
            ->select();
        //foreach ($course_state as $key=>&$value){
            //$value['images'] = explode(',',$value['images']);
        //}
        if (!empty($course_state)) {
            $this->success('获取教学风采列表成功！', $course_state, 200);
        } else {
            $this->error('获取教学风采列表失败！', '', 400);
        }
    }

    /**
     * 获取热门课程列表
     */
    public function getHotCourse(){
        $user = $this->getMember();
        $hot_course = CourseModel::where('status',1)
            ->where('is_hot',1)
            ->where('school_id',$user['school_id'])
            ->order('sort asc,id desc')
            ->limit(4)
            ->select();
        if (!empty($hot_course)) {
            $this->success('获取热门课程列表成功！', $hot_course, 200);
        } else {
            $this->error('获取热门课程列表失败！', '', 400);
        }
    }

    /**
     * 获取所有分类
     */
    public function getAllCate(){
        $cate = CourseCateModel::where(['status'=>1])->order('sort asc,id desc')->select();
        if(!empty($cate)){
            $this->success('获取所有分类成功！',$cate,200);
        }else{
            $this->error('获取所有分类失败！','',401);
        }
    }

    /**
     * 获取所有课程
     */
    public function getAllCourse(){
        $user = $this->getMember();
        $page = $this->request->get('page', 1);
        $cate_id = $this->request->get('cate_id');
        $keyword = $this->request->get('keyword');
        $order = ['sort'=>'asc','id'=>'desc'];
        $course = CourseModel::where('status',1)
//            ->where('type',2)
            ->where('school_id',$user['school_id'])
            // ->where('grade_id='.$user['grade_id'].' or grade_id is null or grade_id=0')
            // ->where('class_id='.$user['class_id'].' or class_id is null or class_id=0')
            ->where(function ($db) use ($cate_id,$keyword) {
                if (!empty($cate_id)) {
                    $db->where('cate_id', $cate_id);
                }
                if (!empty($keyword)) {
                    $db->where('name', 'like', '%' . $keyword . '%');
                }
            })
            ->order($order)
            ->page($page,20)
            ->select()
            ->toArray();
        $this->success('获取所有课程成功！',$course,200);
    }

    /**
     * 获取课程详情
     */
    public function getCourseDetail(){
        $course_id = $this->request->get('course_id');
        $course = CourseModel::where('id',$course_id)->find();
        $course['logo'] = explode('|',$course['logo']);
        $course['intro'] = explode('|',$course['intro']);
        $comment = CourseCommentModel::where('course_id',$course_id)->order('id desc')->limit(2)->select();
        foreach ($comment as $key=>&$value){
            $value['avatar'] = UserModel::where('id',$value['user_id'])->value('avatar');
            $value['nickname'] = UserModel::where('id',$value['user_id'])->value('nickname');
            $value['create_time'] = date('Y-m-d',strtotime($value['create_time']));
        }
        $course['comment'] = $comment;
        $course['comment_total'] = count($comment)?:0;
        $user = $this->getMember();
        $is_add = UserTimesModel::where('user_id',$user['id'])
            ->where('course_id',$course['id'])
            ->find();
        $course['all_userids'] = '';
        if($user['role']==2){
            //常规课找到属于这课程的学生
            if($course['type']==1){
                $course['student'] = UserModel::where('school_id',$course['school_id'])
                    ->where('grade_id',$course['grade_id'])
                    ->where('class_id',$course['class_id'])
                    ->append(['user_id'])
                    ->where('role',1)
                    ->select()
                    ->toArray();
                $course['all_userids'] = UserModel::where('school_id',$course['school_id'])
                    ->where('grade_id',$course['grade_id'])
                    ->where('class_id',$course['class_id'])
                    ->where('role',1)
                    ->field('group_concat(id) as user_ids')->find()['user_ids'];
            }else{
                $course['student'] = UserTimesModel::where('course_id',$course['id'])
                    ->append(['realname'])
                    ->select()
                    ->toArray();
                $course['all_userids'] = UserTimesModel::where('course_id',$course['id'])
                    ->where('user_id','neq',null)
                    ->field('group_concat(user_id) as user_ids')->find()['user_ids'];
            }
        }

        if(!empty($is_add)){
            $course['is_add'] = 1;
        }else{
            $course['is_add'] = 0;
        }
        if(!empty($course)){
            $this->success('获取课程详情成功！',$course,200);
        }else{
            $this->error('获取课程详情失败！','',400);
        }
    }

    /**
     * 获取课程评价
     */
    public function getCourseComment(){
        $course_id = $this->request->get('course_id');
        $comment = CourseCommentModel::where('course_id',$course_id)->order('id desc')->select();
        foreach ($comment as $key=>&$value){
            $value['avatar'] = UserModel::where('id',$value['user_id'])->value('avatar');
            $value['nickname'] = UserModel::where('id',$value['user_id'])->value('nickname');
            $value['create_time'] = date('Y-m-d',strtotime($value['create_time']));
        }
        if(!empty($comment)){
            $this->success('获取课程评价成功！',$comment,200);
        }else{
            $this->error('获取课程评价失败！','',400);
        }
    }

    /**
     * 获取课程考勤详情
     */
    public function getCourseAttendance(){
        $course_id = $this->request->get('course_id');
        $day = date('Y-m-d');
        $attendance = CourseAttendanceModel::where('day',$day)
            ->where('course_id',$course_id)
            ->find();
        $this->success('获取课程考勤详情成功！',$attendance,200);
    }

    /**
     * 获取所有往期课程
     */
    public function getCourseState(){
        $page = $this->request->get('page', 1);
        $cate_id = $this->request->get('cate_id');
        $keyword = $this->request->get('keyword');
        $order = ['id'=>'desc'];
        $course_ids = CourseStateModel::where(1)
            ->where('process',2)
            ->field('group_concat(course_id) as course_ids')->find()['course_ids'];
        $course = CourseModel::where('status',1)
            ->where('id','in',$course_ids)
            ->where(function ($db) use ($cate_id,$keyword) {
                if (!empty($cate_id)) {
                    $db->where('cate_id', $cate_id);
                }
                if (!empty($keyword)) {
                    $db->where('name', 'like', '%' . $keyword . '%');
                }
            })
            ->order($order)
            ->page($page,20)
            ->select()
            ->toArray();

        $this->success('获取所有往期课程成功！',$course,200);
    }


    /**
     * 获取往期课程详情
     */
    public function getCourseStateDetail(){
        $course_id = $this->request->get('course_id');
        $course = CourseModel::where('id',$course_id)->find();
        $course['logo'] = explode('|',$course['logo']);
        $images = CourseStateModel::where('course_id',$course_id)->value('images');
        $course['images'] = explode(',',$images);
        $video = CourseStateModel::where('course_id',$course_id)->value('video');
        $course['video'] = explode(',',$video);
        $desc = CourseStateModel::where('course_id',$course_id)->value('desc');
        $course['desc'] = utf8_decode($desc);
        if(!empty($course)){
            $this->success('获取往期课程详情成功！',$course,200);
        }else{
            $this->error('获取往期课程详情失败！','',400);
        }
    }
}
